/**
 *  *2010-10-8 下午04:38:51  add 
 */
package com.base.myproject.client.ui.celleditor;

import java.util.ArrayList;

import com.extjs.gxt.ui.client.core.El;
import com.extjs.gxt.ui.client.core.XDOM;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.NumberPropertyEditor;
import com.google.gwt.dom.client.InputElement;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;

/**
 * 暂时不能获得焦点，
 * 0表示什么都没选中
 *  1表示选择第一个选项，2 表示第二，4是第三个
 * @author Administrator 2010-10-8 javazgw@gmail.com
 * 构造方法参数如果为true表示不换行，否则换行。RadioGroup同理。by linlepeng@gmail.com 2010-12-20 
 */
public class BaseCheckGroup extends Field<Number> {
	protected El wrap, boxLabelEl;
	protected El[] input;
	//CheckBox cb;
	protected String CheckGroupLabel;
	protected boolean newline;
	int checknum = 0;
	String [] title;
	
	public BaseCheckGroup(String[] title) {
		this.checknum = title.length;
		this.title = title;
		init();
		focusStyle=null;
	}
	
	public BaseCheckGroup(String[] title,boolean newline) {
		this.checknum = title.length;
		this.title = title;
		this.newline = newline;
		init();
		focusStyle=null;
	}
	
	protected void init()
	{
//		this.setHeight("100%");
		this.setHeight(title.length*20+"px");
		input = new El[this.checknum ];
		for(int i = 0; i<this.checknum ;i++)
		{
		
			input[i] = new El(DOM.createInputCheck());
		}
		setFireChangeEventOnSetValue(true);
		value = 0;
		 propertyEditor = new NumberPropertyEditor();
	}

	@Override
	public void clearInvalid() {
		// do nothing
	}

	@Override
	public String getRawValue() {
		if (!rendered) {
			return value.toString();
		}
//		return String.valueOf(((InputElement) getInputEl().dom.cast())
//				.isChecked());
		if(value==null)
			return "0";
		return value.toString();
	}

	@Override
	public void markInvalid(String msg) {

	}

	public void setBoxLabel(String CheckGroupLabel) {
		this.CheckGroupLabel = CheckGroupLabel;
		if (rendered) {
			boxLabelEl.update(getBoxLabel());
		}
	}

	@Override
	protected void initValue() {
		if (value != null) {
			setRawValue(value.toString());
		}
	}
	 public Number getValue() {
		    if (!rendered) {
		      return value;
		    }
		    String v = getRawValue();
		    if (emptyText != null && v.equals(emptyText)) {
		      return null;
		    }
		    if (v == null || v.equals("")) {
		      return null;
		    }
		    try {
		      return propertyEditor.convertStringValue(v);
		    } catch (Exception e) {
		      return null;
		    }
		  }

	 public Object[] getValueArray()
	 {
		 
		 ArrayList<String> al = new ArrayList<String>();
		int v = getValue()==null?0:getValue().intValue();
		 for(int i=0;i<title.length;i++)
		 {
			 if((v & (1<<i)) ==(1<<i))
			 {
				 al.add(title[i]);
			 }
		 }
		 return al.toArray();
	 }
	public void setValue(Number value) {

		Number oldValue = this.value;
		this.value = value;
		if (rendered) {
			Number v;// = value == null ? "" :
			// propertyEditor.getStringValue(value);
			try {
				int vtemp = (int)Double.parseDouble(value+"");
				
				

				v =value;
				
				for(int i = 0;i<checknum;i++)
				{
					
					((InputElement)input[i].dom.cast()).setChecked(((1<<i)&vtemp)==(1<<i));

				}
				setRawValue(v+"");
				validate();

			} catch (NumberFormatException e) {
				setRawValue("0");
			}

		}
		if (((oldValue == null && value != null) || (oldValue != null && !oldValue
				.equals(value)))) {
			fireChangeEvent(oldValue, value);
		}

	}


	protected void onClick(ComponentEvent ce) {
		if (readOnly) {
			ce.stopEvent();
			return;
		}
		int value = 0;
		for(int i=0;i<this.checknum;i++)
		{
			
			
			if(((InputElement)input[i].dom.cast()).isChecked())
			{
				value =value | (1<<i);
			}
		}
//		boolean v = getInputEl().dom.getPropertyBoolean("checked");
//		boolean v = getInputEl().dom.get
		setValue(value);
		
	}

	protected void onRender(Element target, int index) {
		 if (el() == null) {
			  _onRender1(target,index);
		    }
		    super.onRender(target, index);
		    if(value.intValue()!=0)
		    {
		    	setValue(value);
		    }
	}
	protected void _onRender1(Element target, int index){
		Element e = DOM.createDiv();
		El tr = null;
		e.setPropertyString("hideFocus", "hideFocus");
		e.setClassName("x-form-check-wrap");
		El table = new El(DOM.createTable());
		Element   body = DOM.createTBody();
		table.appendChild(body);
		if(newline){
			tr = new El(DOM.createTR());
		}
		for(int i=0;i<checknum;i++){ 
		    input[i].setId(XDOM.getUniqueId());
		    if(!newline){
		    	tr = new El(DOM.createTR());
		    }
		    El  td = new El(DOM.createTD());
		    El  td2 = new El(DOM.createTD());
		    body.appendChild(tr.dom);
		   
		    tr.appendChild(td2.dom);
		    tr.appendChild(td.dom);
		    El  boxLabelEl = new El(DOM.createLabel());
		    boxLabelEl.setStyleAttribute("position", "static");
		    boxLabelEl.setElementAttribute("for", input[i].getId());
		    boxLabelEl.setElementAttribute("htmlFor", input[i].getId());
		    boxLabelEl.dom.setClassName("x-form-cb-label");
		    boxLabelEl.update(this.title[i]);
		    td2.appendChild(input[i].dom);
		    td.appendChild(boxLabelEl.dom);
		     
		}
		e.appendChild(table.dom);
		setElement(e, target, index);
	}
	
	protected void _onRender(Element target, int index)
	{
		wrap = new El(DOM.createDiv());
	    wrap.dom.setPropertyString("hideFocus", "hideFocus");
	    wrap.dom.setClassName("x-form-check-wrap");
	    El table = new El(DOM.createTable());
	    
	    for(int i=0;i<checknum;i++)
	    {
	    	input[i].setId(XDOM.getUniqueId());
	    El  tr = new El(DOM.createTR());
	    El  td = new El(DOM.createTD());
	    El  td2 = new El(DOM.createTD());
	    table.appendChild(tr.dom);
	   
	    tr.appendChild(td2.dom);
	    tr.appendChild(td.dom);
	    El  boxLabelEl = new El(DOM.createLabel());
	      boxLabelEl.setStyleAttribute("position", "static");
	      boxLabelEl.setElementAttribute("for", input[i].getId());
	      boxLabelEl.setElementAttribute("htmlFor", input[i].getId());
	      boxLabelEl.dom.setClassName("x-form-cb-label");
	      boxLabelEl.update(this.title[i]);
	      td2.appendChild(input[i].dom);
	      td.appendChild(boxLabelEl.dom);
	     
	    }
	    wrap.dom.appendChild(table.dom);
	   setElement(wrap.dom, target, index);
	    
	  
	}

/*	protected void onResize(int width, int height) {
		super.onResize(width, height);
		alignElements();
	}*/

	public String getBoxLabel() {
		return CheckGroupLabel;
	}
}
